#include "symbolTable.h"

Symbol* symbol_new() {
	Symbol *new;
	
	new = (Symbol*) malloc(sizeof(Symbol));
	new->next = NULL;
	new->prev = NULL;
	return new;
}

/*-------------------------------------------------------------*/

int symbol_isEmpty(Symbol *table) {
	if(table == NULL) {
		return 1;
	}
	else {
		return 0;
	}
}

/*-------------------------------------------------------------*/

Symbol* symbol_search(Symbol *table, char *name) {
	if (symbol_isEmpty(table)) {
		return NULL;
	}
	while (table != NULL) {
		if (strcmp(table->name, name)==0) {
			return table;
		}
		table = (Symbol *) table->next;
	}
	return NULL;
}

/*-------------------------------------------------------------*/

int symbol_isAdded(char str[64]) {
	Symbol *p;
	p = head;
	while (p != NULL) {
		if (strcmp(p->name, str)==0) {
			return 1;
		}
		p = (Symbol *) p->next;
	}
	return 0;
}

/*-------------------------------------------------------------*/

void symbol_addFromNode(Symbol **table, TreeNode *node, TreeNode *type) {
	Symbol *new;
	
	while (node != NULL) {
		if (symbol_isEmpty(*table)) {
		new = symbol_new();
		strcpy(new->name, node->name);
		new->type = get_type(type->name);
		*table = new;
		}
		else {
			if (!symbol_isAdded(node->name)) {
				new = symbol_new();
				strcpy(new->name, node->name);
				new->type = get_type(type->name);
				// TODO inserir valor
				(*table)->prev = (Symbol *) new;
				new->next = (Symbol *) *table;
				*table = new;
			}
		}
		node = node->sibling;
	}
}

int symbol_add(Symbol **table, char name[64]) {
	Symbol *new;
	if (symbol_isEmpty(*table)) {
		new = symbol_new();
		strcpy(new->name, name);
		*table = new;
	}
	else {
		if (!symbol_isAdded(name)) {
			new = symbol_new();
			strcpy(new->name, name);
			// TODO inserir valor
			(*table)->prev = (Symbol *) new;
			new->next = (Symbol *) *table;
			*table = new;
			return 1;
		}
	}
	return 0;
}

t_types get_type(char *type) {
	if (strcmp("inteiro", type)==0) {
		return INTEIRO;
	}
	else if (strcmp("real", type)==0) {
		return REAL;
	}
	else if (strcmp("booleano", type)==0) {
		return BOOLEANO;
	}
	else if (strcmp("texto", type)==0) {
		return TEXTO;
	}
	else {
		return VOID;
	}
}

/*-------------------------------------------------------------*/

t_types symbol_getType(Symbol *p) {
	return p->type;
}

/*-------------------------------------------------------------*/

t_types symbol_getTypeByName(Symbol *table, char *name) {
	return symbol_search(table, name)->type;
}

/*-------------------------------------------------------------*/

int symbol_sameType(Symbol *p1, Symbol *p2) {
	if (symbol_getType(p1) == symbol_getType(p2)) {
		return 1;
	}
	else {
		return 0;
	}
}

/*-------------------------------------------------------------*/

int symbol_sameTypeByName(Symbol *table, char *name1, char *name2) {
	if (symbol_getTypeByName(table, name1) == symbol_getTypeByName(table, name2)) {
		return 1;
	}
	else {
		return 0;
	}
}

/*-------------------------------------------------------------*/

void symbol_print(Symbol *symbol) {
	if (!symbol_isEmpty(symbol)) {
		printf("symTab: %s - %d\n", symbol->name, symbol->type);
	}
}

/*-------------------------------------------------------------*/

void symbol_printTable(Symbol *table) {
	while (!symbol_isEmpty(table)) {
		symbol_print(table);
		table = (Symbol *) table->next;
	}
}

